//
//  YJAlertView.m
//  CToolBox
//
//  Created by crj on 2016/11/16.
//  Copyright © 2016年 oyf. All rights reserved.
//

#import "YJAlertView.h"

#define CancleTag 100

@interface YJAlertView()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *messaeLabel;


@property (nonatomic, strong) NSArray *btnBGColors;
@property (nonatomic, strong) NSArray *btnTitleColors;

@property (nonatomic, assign) CGFloat contentHeight;
@property (nonatomic, assign) CGFloat titleHeight;
@property (nonatomic, assign) CGFloat footerHeight;
@end

@implementation YJAlertView

- (void)setConfig{
    
    
    _btnBGColors = @[[UIColor whiteColor],
                HEXCOLOR(kColorBlue1, 1)];
    _btnTitleColors = @[HEXCOLOR(kColorGray2, 1),
                        [UIColor whiteColor]];
    
    _contentHeight = 170*kScreenScale;
    _titleHeight = 40*kScreenScale;
    _footerHeight = 50*kScreenScale;
}

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message buttonTitles:(NSArray *)buttontitles
{
    self = [super init];
    if (self) {
        
        [self setConfig];
        self.frame = [UIApplication sharedApplication].keyWindow.bounds;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        
        [self addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        
        _contentView = [[UIView alloc] init];
        _contentView.frame = CGRectMake(0, HEIGHT, WIDTH, _contentHeight);
        _contentView.backgroundColor = HEXCOLOR(kColorGray7, 1);
        
        if(title){
            _titleLabel = [[UILabel alloc] init];
            _titleLabel.frame = CGRectMake(0, 0, WIDTH, _titleHeight);
            _titleLabel.font = [UIFont boldSystemFontOfSize:15];
            _titleLabel.textColor = HEXCOLOR(kColorBlack1, 1);
            _titleLabel.textAlignment = NSTextAlignmentCenter;
            _titleLabel.text = title;
            [_contentView addSubview:_titleLabel];
        }
        
        _messaeLabel = [[UILabel alloc] init];
        if(_titleLabel){
            _messaeLabel.frame = CGRectMake(0, _titleHeight, WIDTH, _contentHeight-_footerHeight-_titleHeight);

        }else{
        _messaeLabel.frame = CGRectMake(0, 0, WIDTH, _contentHeight-_footerHeight);
        }
        _messaeLabel.numberOfLines = 0;
        _messaeLabel.font = [UIFont systemFontOfSize:15];
        _messaeLabel.textColor = HEXCOLOR(kColorBlack2, 1);
        _messaeLabel.textAlignment = NSTextAlignmentCenter;
        _messaeLabel.text = message;
        
        [self addSubview:_contentView];
        [_contentView addSubview:_messaeLabel];
        
        CGFloat btnWidth = WIDTH/buttontitles.count;
        for(int i=0; i<buttontitles.count; i++){
            
            NSString *btnTitle = buttontitles[i];
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = i;
            [button setTitle:btnTitle forState:UIControlStateNormal];
            button.frame = CGRectMake(btnWidth*i, _contentHeight-_footerHeight, btnWidth, _footerHeight);
            
            if(buttontitles.count==1){
                [button setBackgroundColor:_btnBGColors[1]];
                [button setTitleColor:_btnTitleColors[1] forState:UIControlStateNormal];

            }else{
                [button setBackgroundColor:_btnBGColors[i]];
                [button setTitleColor:_btnTitleColors[i] forState:UIControlStateNormal];
}
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            [_contentView addSubview:button];
            
            [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    return self;
}


+ (YJAlertView *)showAlertWithTitle:(NSString *)title message:(NSString *)message buttonTitles:(NSArray *)buttonTitles{

    YJAlertView *alert = [[YJAlertView alloc] initWithTitle:title message:message buttonTitles:buttonTitles];
    [alert show];
    return alert;
}

- (void)show{
    
    WeakSelf;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.contentView.frame = CGRectMake(0, HEIGHT-_contentHeight, WIDTH, _contentHeight);
        weakSelf.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    }];

}

- (void)close{
    
    WeakSelf;
    [UIView animateWithDuration:0.3 animations:^{
        
        weakSelf.contentView.frame = CGRectMake(0, HEIGHT, WIDTH, _contentHeight);
        weakSelf.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];

    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

- (void)cancelAction{
    
    if([_delegate respondsToSelector:@selector(alertSelectWithAlert:indext:)]){
        [_delegate alertSelectWithAlert:self indext:CancleTag];
    }
    [self close];
}

- (void)btnAction:(UIButton *)btn{
    
    NSLog(@"btnSelect = %zi",btn.tag);
    if([_delegate respondsToSelector:@selector(alertSelectWithAlert:indext:)]){
        [_delegate alertSelectWithAlert:self indext:btn.tag];
    }
    [self close];
}


@end
